<template>
  <div class="info-panel">
    <div class="info-content">
      <div class="info-item">
        <span class="info-label">课题名称：</span>
        <span class="info-value">{{ info.projectName || '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">合同来源：</span>
        <span class="info-value">{{ info.contractSource || '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">合同编号：</span>
        <span class="info-value">{{ info.contractNumber || '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">到账经费(万)：</span>
        <span class="info-value">{{ info.fundingAmount !== null ? info.fundingAmount : '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">课题状态：</span>
        <span class="info-value">{{ info.projectStatus || '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">开始时间：</span>
        <span class="info-value">{{ formatProjectTime(info.startTime) || '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">本人排名：</span>
        <span class="info-value">{{ info.rank !== null ? info.rank : '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">截止时间：</span>
        <span class="info-value">{{ formatProjectTime(info.endTime) || '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">共同作者：</span>
        <span class="info-value">{{ formatAuthors(info.authorInfo) || '暂无' }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">成果归属：</span>
        <span class="info-value">{{ info.remarks || '暂无' }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  info: {
    type: Object,
    default: () => ({})
  }
})

// 格式化课题时间
const formatProjectTime = (time) => {
  if (!time) return null;

  try {
    const date = new Date(time);
    if (isNaN(date.getTime())) return null;

    // 格式化为YYYY-MM-DD
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');

    return `${year}-${month}-${day}`;
  } catch (e) {
    console.error('时间格式化错误:', e);
    return null;
  }
}

// 格式化共同作者信息
const formatAuthors = (authors) => {
  if (!authors) return null;

  try {
    // 处理字符串类型的authorInfo
    if (typeof authors === 'string') {
      try {
        authors = JSON.parse(authors);
      } catch (e) {
        return authors; // 如果不是JSON字符串，直接返回原值
      }
    }

    // 处理数组类型的authorInfo
    if (Array.isArray(authors)) {
      // 提取name字段并用顿号连接
      const names = authors.map(author => {
        if (typeof author === 'object' && author !== null) {
          return author.name || '';
        }
        return '';
      }).filter(name => name.trim());

      return names.length > 0 ? names.join('、') : null;
    }

    return null;
  } catch (e) {
    console.error('作者信息格式化错误:', e);
    return null;
  }
}
</script>

<style scoped>
.info-panel {
  height: 100%;
}

.info-content {
  padding: 16px;
  max-height: 400px;
  overflow-y: auto;
}

.info-item {
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.info-label {
  font-size: 12px;
  color: #9aa0a6;
  font-weight: 500;
}

.info-value {
  font-size: 13px;
  color: #e8eaed;
  word-break: break-all;
  line-height: 1.4;
}

.info-content::-webkit-scrollbar {
  width: 8px;
}

.info-content::-webkit-scrollbar-track {
  background: #323639;
}

.info-content::-webkit-scrollbar-thumb {
  background: #5f6368;
  border-radius: 4px;
}

.info-content::-webkit-scrollbar-thumb:hover {
  background: #80868b;
}
</style>
